/**Transcore is a remote-GUI to Transmission.
Copyright (C) 2009 by Jerry de Swart

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package transcore;

import java.io.IOException;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author J de Swart (Custombuild@gmail.com)
 */
public class DaemonStats {

	private int totalTorrents;
	private int activeTorrents;
	private int pausedTorrents;
	private int downloadSpeed;
	private int uploadSpeed;

	private JSONObject cumulativestats;
	private int cumulativeUploadedBytes;
	private int cumulativeDownloadedBytes;
	private int cumulativeFilesAdded;
	private int cumulativeSessionCount;
	private int cumulativeSecondsActive;
	
	private JSONObject currentstats;
	private int currentUploadedBytes;
	private int currentDownloadedBytes;
	private int currentFilesAdded;
	private int currentSessionCount;
	private int currentSecondsActive;

	public DaemonStats() {
	}

	public void update(TransConnection conn) throws IOException {
		JSONMessage updateMessage = new JSONMessage(TransJSON.SESS_STATS);

		try {
			JSONObject response = conn.send(updateMessage);

			JSONObject sessionStats = response.getJSONObject(TransJSON.ARGS);
//			TODO: Handling of transmission versions < 7696
//			JSONObject sessionStats = args.getJSONObject(TransJSON.SESS_STATS);

			totalTorrents = sessionStats.getInt(TransJSON.TOTAL_TORR);
			activeTorrents = sessionStats.getInt(TransJSON.ACTIVE_TORR);
			pausedTorrents = sessionStats.getInt(TransJSON.PAUSED_TORR);
			downloadSpeed = sessionStats.getInt(TransJSON.TOTAL_DOWNSPEED);
			uploadSpeed = sessionStats.getInt(TransJSON.TOTAL_UPSPEED);

			cumulativestats = sessionStats.getJSONObject(TransJSON.CUMUL_STATS);
			cumulativeUploadedBytes = cumulativestats.getInt(TransJSON.UPL_BYTES);
			cumulativeDownloadedBytes = cumulativestats.getInt(TransJSON.DOWNL_BYTES);
			cumulativeFilesAdded = cumulativestats.getInt(TransJSON.FILES_ADDED);
			cumulativeSessionCount = cumulativestats.getInt(TransJSON.SESS_COUNT);
			cumulativeSecondsActive = cumulativestats.getInt(TransJSON.SEC_ACTIVE);
			
			currentstats = sessionStats.getJSONObject(TransJSON.CURRENT_STATS);
			currentUploadedBytes = currentstats.getInt(TransJSON.UPL_BYTES);
			currentDownloadedBytes = currentstats.getInt(TransJSON.DOWNL_BYTES);
			currentFilesAdded = currentstats.getInt(TransJSON.FILES_ADDED);
			currentSessionCount = currentstats.getInt(TransJSON.SESS_COUNT);
			currentSecondsActive = currentstats.getInt(TransJSON.SEC_ACTIVE);
		} catch (JSONException e) {
			//TODO: Should be debug-error
			System.out.println(e.getMessage());
		}

		//TODO: Remove BUGREPORT INFO:
		//System.out.println(response);

	}

	@Deprecated
	public HashMap<String, Integer> getDaemonStats(){
		HashMap daemonStats = new HashMap<String, Integer>();

		daemonStats.put("totalTorrents", new Integer(totalTorrents));
		daemonStats.put("activeTorrents", new Integer(activeTorrents));
		daemonStats.put("pausedTorrents", new Integer(pausedTorrents));
		daemonStats.put("downloadSpeed", new Integer(downloadSpeed));
		daemonStats.put("uploadSpeed", new Integer(uploadSpeed));

		return daemonStats;
	}

	@Override
	public String toString() {
		String returnString = "Total torrents: " + totalTorrents + "\n";
		returnString += "Active torrents: " + activeTorrents + "\n";
		returnString += "Paused torrents: " + pausedTorrents + "\n";
		returnString += "Global Downloadspeed: " + downloadSpeed + "\n";
		returnString += "Global Uploadspeed: " + uploadSpeed + "\n";
		return returnString;
	}

	public int getTotalTorrents(){
		return totalTorrents;
	}

	public int getActiveTorrents(){
		return activeTorrents;
	}

	public int getPausedTorrents(){
		return pausedTorrents;
	}

	public int getDownloadSpeed(){
		return downloadSpeed;
	}

	public int getUploadSpeed(){
		return uploadSpeed;
	}

	public int getCumulativeUploadedBytes(){
		return cumulativeUploadedBytes;
	}

	public int getCumulativeDownloadedBytes(){
		return cumulativeDownloadedBytes;
	}

	public int getCumulativeFilesAdded(){
		return cumulativeFilesAdded;
	}

	public int getCumulativeSessionCount(){
		return cumulativeSessionCount;
	}

	public int getCumulativeSecondsActive(){
		return cumulativeSecondsActive;
	}

	public int getCurrentUploadedBytes(){
		return currentUploadedBytes;
	}

	public int getCurrentDownloadedBytes(){
		return currentDownloadedBytes;
	}

	public int getCurrentFilesAdded(){
		return currentFilesAdded;
	}

	public int getCurrentSessionCount(){
		return currentSessionCount;
	}

	public int getCurrentSecondsActive(){
		return currentSecondsActive;
	}
}
